//
//  WmcSheetView.swift
//  YPWatermarkCamera
//
//  Created by 赵新 on 2022/4/5.
//

import UIKit
import YPBedrock

// MARK: - [WmcSheetView]

class WmcSheetView: WmcSelectView {
    // Internal

    /// 点击索引回调
    var didSelectIndexBlock: YPBlock.OneParam<Int>?

    /// 选中某一个
    var selectedIndex: Int?

    /// 元素集合
    var items: [String] = [] {
        didSet {
            dataSource = items
            dataSource.append("取消")
            tableView.reloadData()
        }
    }

    override func makeUI() {
        super.makeUI()

        addSubview(tableView)
        tableView.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.top.equalTo(titleView.snp.bottom)
        }
    }

    // Private

    private var dataSource: [String] = []

    private lazy var tableView: UITableView = {
        let view = UITableView.YP.Create(frame: .zero,
                                         style: .plain,
                                         delegate: self,
                                         dataSource: self,
                                         registers: [
                                             YPRegister(WmcSheetViewCell.self),
                                         ])
        view.backgroundColor = .wmc_white_FFFFFF
        view.tableFooterView = UIView()
        return view
    }()
}

// MARK: - UITableViewDelegate

extension WmcSheetView: UITableViewDelegate {
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if indexPath.row < dataSource.count - 1 {
            dismiss() { [weak self] in
                guard let weakSelf = self else { return }
                weakSelf.didSelectIndexBlock?(indexPath.row)
            }
        }
        dismiss() {
            
        }
    }
}

// MARK: - UITableViewDataSource

extension WmcSheetView: UITableViewDataSource {
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return .YP.Min
    }

    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return .YP.Min
    }

    func tableView(_: UITableView, numberOfRowsInSection _: Int) -> Int {
        return dataSource.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell: WmcSheetViewCell = tableView.yp.dequeueReusableCell()
        cell.selectionStyle = .none

        if let text = dataSource.yp.object(at: indexPath.row) {
            cell.titleLabel.text = text
        }

        let color = selectedIndex == indexPath.row ? UIColor.wmc_mainTheme : UIColor.wmc_grayH2_323233
        cell.titleLabel.textColor = color
        return cell
    }

    func tableView(_: UITableView, heightForRowAt _: IndexPath) -> CGFloat {
        return 54
    }
}

// MARK: - WmcAlertViewProtocol

extension WmcSheetView: WmcAlertViewProtocol {
    /// 显示
    func show(height: CGFloat? = nil, completion: YPBlock.Empty? = nil) {
        let config: WmcPresentVC.Config =
            .basicConfig(view: self, style: .sheet(), layoutBlock: { make in
                make.left.right.bottom.equalToSuperview()
                if let height = height {
                    make.height.equalTo(height)
                }else{
                    make.height.equalToSuperview().multipliedBy(0.4)
                }
            })
        show(config: config, completion: { [weak self] in
            guard let weakSelf = self else { return }
            if let selectedIndex = weakSelf.selectedIndex {
                weakSelf.tableView.scrollToRow(at: .init(row: selectedIndex, section: 0), at: .middle, animated: true)
            }
            completion?()
        })
    }
}
